package com.qf.shop.service.impl;


import com.qf.shop.dao.AddressDao;
import com.qf.shop.dao.Impl.AddressDaoImpl;
import com.qf.shop.entity.Address;
import com.qf.shop.service.AddressService;
import com.qf.shop.utils.DruidUtils;

import java.util.List;

public class AddressServiceImpl implements AddressService {
    private AddressDao addressDao = new AddressDaoImpl();

    @Override
    public List<Address> getAllAddressByUid(int uid) {
        List<Address> addressList = null;
        try {
            DruidUtils.begin();
            addressList = addressDao.selectAll(uid);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return addressList;
    }

    @Override
    public int insertAddress(Address address) {
        int result = 0;
        try {
            DruidUtils.begin();
            result = addressDao.insert(address);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateAddress(Address address) {
        int result = 0;
        try {
            DruidUtils.begin();
            result = addressDao.update(address);
            DruidUtils.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public int deleteAddress(int id) {
        int result = 0;
        try {
            DruidUtils.begin();
            result = addressDao.delete(id);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int serDefaultAddress(int uid, int id) {
        int result = 0;
        try {
            DruidUtils.begin();
            //1.修改指定id的地址的level为1.
            result = addressDao.updateDefault(id);
            if(result>0){
                //2.将当前用户的非指定id的地址改为0
                addressDao.updateCommns(id,uid);
            }
            DruidUtils.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
